package cn.teduymdj.jxc.service.impl;

import cn.teduymdj.jxc.ex.ServiceException;
import cn.teduymdj.jxc.mapper.DamageListGoodsMapper;
import cn.teduymdj.jxc.mapper.DamageListMapper;
import cn.teduymdj.jxc.pojo.dto.DamageListAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.DamageListGoodsAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.SelectConditionDTO;
import cn.teduymdj.jxc.pojo.entity.DamageList;
import cn.teduymdj.jxc.pojo.entity.DamageListGoods;
import cn.teduymdj.jxc.pojo.vo.DamageListGoodsListItemVO;
import cn.teduymdj.jxc.pojo.vo.DamageListListItemVO;
import cn.teduymdj.jxc.service.DamageListGoodsService;
import cn.teduymdj.jxc.util.DateUtil;
import cn.teduymdj.jxc.util.StringUtil;
import cn.teduymdj.jxc.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 处理报损货物表相关业务的实现类
 *
 * @author 陈坤鹏
 */
@Slf4j
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsMapper damageListGoodsMapper;
    @Autowired
    private DamageListMapper damageListMapper;

    public DamageListGoodsServiceImpl() {
        log.debug("创建业务对象:DamageListGoodsServiceImpl");
    }

    @Override
    public void save(DamageListAddNewDTO damageListAddNewDTO, List<DamageListGoodsAddNewDTO> damageListGoodsAddNewDTOS) {
        log.debug("处理添加报损单的业务:{}", damageListAddNewDTO);
        int damageNumber = damageListMapper.countByDamageNumber(damageListAddNewDTO.getDamageNumber());
        // 查询单号是否存在
        if (damageNumber > 0) {
            // 是:添加失败，抛出异常，单号已存在
            String message = "添加失败，报损单号已存在，请重新审查";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        int result = damageListMapper.addNew(damageListAddNewDTO);
        // 判断是否添加成功
        if (result != 1) {
            // 否:添加失败，抛出异常，请稍后重试
            String message = "添加失败，服务器忙，请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        for (DamageListGoodsAddNewDTO damageListGoodsAddNewDTO : damageListGoodsAddNewDTOS) {
            damageListGoodsAddNewDTO.setDamageListId(damageListAddNewDTO.getId());
        }
        damageListGoodsMapper.addNewBatch(damageListGoodsAddNewDTOS);
    }

    @Override
    public List<DamageListListItemVO> getDamageList(SelectConditionDTO selectConditionDTO) {
        List<DamageListListItemVO> list = damageListMapper.listByTime(selectConditionDTO);
        return list;
    }

    @Override
    public List<DamageListGoodsListItemVO> getDamageListGoodsByDamageListId(Long damageListId) {
        return damageListGoodsMapper.countByDamageListId(damageListId);
    }

    @Override
    public DamageListListItemVO getDamageListById(Long damageListId) {
        return damageListMapper.countByDamageList(damageListId);
    }
}
